<?php
namespace app\admin\controller;
use think\Db;
/**
 * Created by PhpStorm.
 * User: LENOVO
 * Date: 2017/12/1
 * Time: 13:34
 */
use \app\admin\AdminCommon;
class Friends extends AdminCommon{
    //加载友情链接
    public function getindex(){
     //创建请求
        $request=request();
     //查询出数据
        $data=Db::table('tp_friends')->select();
        //$data[$i]=$data;
       // $i++;
        return $this->fetch("friends/index",["data"=>$data,'b'=>1]);
         }
    //添加友情链接
         public function getadd(){
           return $this->fetch("friends/add");
          }
    //执行添加
        public function postDoadd(){
             //创建请求
                $request=request();
        //获取参数
            $data=$request->only(['friends_name','friends_path','friends_status','friends_sta']);
         //把路径赋值给$path
            $path=$data['friends_path'];
         //判断前缀是否是http://和https://
            if ((substr($path, 0, 7) != 'http://') && (substr($path, 0, 8) != "https://")) {
         //如不是就拼接
                $path = "http://" .$path;
            }
          //把赋值的结果承储起来
            $data["friends_path"] = $path;
          //调用validate
            $result=$this->validate($request->param(),'Friends');
            if(true!==$result){
                $this->error($result,'friends/add');
                   }
           //把数据承储到数据库里
                   $info=Db::table("tp_friends")->insert($data);
                   //var_dump($s) ;exit;
                    if($info){
                       $this->success("添加成功",'friends/index');
                    }else{
                        $this->error("添加失败",'friends/add');
                    }

                   }
            //执行删除方法
                    public function getDelete(){
             //获取请求方式
                        $request=request();
                        $id=$request->param('id');
                       $data= Db::table("tp_friends")->where('id',$id)->delete();
             //判断是否删除成功
                        if(!$data){
                           $this->error("删除失败","friends/index");
                        }else{
                            $this->success("删除成功","friends/index");
                        }

                    }
             //修改
                    public function getedit(){
              //创建参数
                        $request=request();
               // 获取id值
                        $id=$request->param("id");
                //根据id查询出数据
                     $data=Db::table("tp_friends")->where('id',$id)->find();
                     //var_dump($data);exit;
                      return $this->fetch("friends/edit",["data"=>$data]);
                    }
            //执行修改
                    public function postDoedit(){
                        $request=request();
                        $data=$request->param();
                        //var_dump($info);
             //清楚多余的字段
                        unset($data['action']);
                        //var_dump($info);exit;
                        $result=Db::table("tp_friends")->where("id",$data['id'])->update($data);
                        if ($result) {
                            $data = [
                                'status' => 1,
                                'msg' => '添加成功',
                            ];
                        } else {
                            $data = [
                                'status' => 0,
                                'msg' => '添加失败',
                            ];
                        };
                        return  json($data);
                    }
                //禁用
                    public function poststop(){
                //获取参数
                            $request=request();
                            $res=$request->param();
                            unset($res['action']);
                            $result=Db::table("tp_friends")->where('id',$res['id'])->update(['friends_status'=>'0']);
                            if($result){
                                $data=[
                                    'status' => 1,
                                    'msg' => '已隐藏',
                                    'id'=>$res['id'],
                                ];
                            }else{
                                $data=[
                                    'status' => 0,
                                    'msg' => '操作失败，请稍后重试',
                                    'id'=>$res['id'],
                                ];
                            }

                            return $data;
                    }
                //显示
                    public function poststart(){
                        $request=request();
                        $res=$request->param();
                        unset($res['action']);
                        $result=Db::table("tp_friends")->where('id',$res['id'])->update(['friends_status'=>'1']);
                        if($result){
                            $data=[
                                'status' => 1,
                                'msg' => '已显示',
                                'id'=>$res['id'],
                            ];
                        }else{
                            $data=[
                                'status' => 0,
                                'msg' => '操作失败，请稍后重试',
                                'id'=>$res['id'],
                            ];
                        }

                        return $data;
                    }
                //批量删除
                    public function postdatadel(){
                            $request=request();
                            $data=$request->param();
                            unset($data['action']);
                           //print_r($data);exit;
                        $ids=explode(",",$data['ids']);
                        Db::startTrans();
                        try{
                            foreach ($ids as $id ){
                             $result=Db::table("tp_friends")->where('id',$id)->delete();
                            }
                            // 提交事务
                            Db::commit();
                        } catch (\Exception $e) {
                            // 回滚事务
                            Db::rollback();
                        }
                        if($result){
                            $data=[
                                'status' => 1,
                                'msg' => '删除成功',
                            ];
                        }else{
                            $data=[
                                'status' => 0,
                                'msg' => '删除失败',
                            ];
                       }
                       return $data;
                    }

        }

